package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R add(@RequestBody Category category, HttpSession session) {
        log.info("category:{}", category);
        //增加分类
        categoryService.save(category);
        return R.success("新增分类成功");
    }


    @GetMapping("/page")
    public R page(int page,int pageSize) {
        //分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);
        categoryService.page(pageInfo);
        return R.success(pageInfo);
    }
    @DeleteMapping
    public R delete(Long id){
        //删除分类加了一个判断接口
        R remove = categoryService.remove(id);
        return remove;
    }

    @PutMapping
    public R update(@RequestBody Category category){
        //分类修改
        categoryService.updateById(category);
        return R.success("分类修改成功");
    }

    /*
    查询菜品分类列表数据
     */
    @GetMapping("/list")
    public R query( Category category ){
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //条件构造
        lambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        //排序
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        //装入集合
        List<Category> list = categoryService.list(lambdaQueryWrapper);

        return R.success(list);
    }



}
